# -*- coding: utf-8 -*-
from openerp import api, fields, models
import addons.decimal_precision as dp
import pdb

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    sale_document_datas = fields.Binary(string='附件', attachment=True)
    sale_document_fname = fields.Char('附件名', index=True)
    discount = fields.Float(string='Discount (%)', digits=dp.get_precision('Discount'), default=0.0)
    route_id=fields.Many2one('stock.location.route', string='Route', domain=[('sale_selectable', '=', True)],
                    ondelete='restrict')
    remark = fields.Char(string='备注')

    @api.onchange('discount','route_id')
    def order_line_change(self):
        for l in self.order_line:
            l.discount=self.discount
            l.route_id=self.route_id.id

    def action_import(self):
        return {
            'name': '报价单导入',
            'res_model': 'import.quotation.wizard',
            'type': 'ir.actions.act_window',
            'view_mode': 'form',
            'view_id': self.env.ref('mlupdate.view_import_excel_wizard').id,
            'target': 'new',
            'context': {'order_id': self.id},
        }


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'
    '''将price_total改成非只读，可以输入数据。并且倒算discount .
      订单中显示price_total（含税） 而不是price_subtotal(不含税)，可以在开票中设置，勾选含税即可
    '''
    # _order = 'order_id,name,sequence, id'

    price_total = fields.Monetary(compute='_compute_amount', string='Total',
                                  readonly=False, store=True) #设置为可写

    categ_name = fields.Char('品牌',related='product_id.categ_id.name')
    date_order = fields.Datetime('订单日期',related='order_id.date_order',store=True)
    price_deal = fields.Float('实际成交价', digits=dp.get_precision('Product Price'), compute='_compute_price_deal')
    cust_partner_id = fields.Many2one('res.partner',store=True, string='公司', readonly=False,compute='_compute_partner_id')
    # default_code=fields.Char('产品编号',related='product_id.default_code')
    default_code2=fields.Char('料号',related='product_id.default_code2')
    product_name=fields.Char('Description',related='product_id.product_name',store=True)

    @api.depends('order_id.partner_id')
    def _compute_partner_id(self):
        for order_line in self:
            order_line.cust_partner_id=order_line.order_id.partner_id.parent_id.id or order_line.order_id.partner_id.id


    @api.depends('price_total','product_uom_qty','discount')
    def _compute_price_deal(self):
        for l in self:
            l.price_deal = l.product_uom_qty and l.price_total/l.product_uom_qty

    # 还是用原来的dp.get_precision('Discount')，在前台将2位改成12位。因为开票等也需要用到discount
    # discount = fields.Float(string='Discount (%)', digits=dp.get_precision('discount-dbh'), default=0.0) #输入总计倒算discount,discount的小数位数为12为而非2位，保证总计数值不变

    #当所选产品有最小订货量时，报价单数量改成最小订货量,取起订量及询价数量最大值,导入时也已处理,这里只对界面操作的处理，即客户端)
    @api.onchange('product_id')
    def _get_product_uom_qty(self):
        if self.product_id:
            supplierinfo=self.product_id.product_tmpl_id.seller_ids
            # if len(supplierinfo) ==0 :
            if not supplierinfo:
                qty=1.0
            else:
                qty=max(max(supplierinfo.mapped('min_qty')),self.product_uom_qty) #多家供应商时，取最大的那个最小订货量
            self.product_uom_qty=qty

    @api.onchange('price_total')
    @api.multi
    def _compute_discount(self):
        for line in self:
            if line.product_uom_qty and line.price_unit:
                # pdb.set_trace()
                price = line.price_total / line.product_uom_qty
                line.discount = (1 - price / line.price_unit) * 100

    # price_show = fields.Monetary(compute='_compute_price_show', string='含税单价')  # 用于打印的报价单显示单价，这时不显示折扣
    # @api.depends('price_total')
    # @api.multi
    # def _compute_price_show(self):
    #     for line in self:
    #         if line.product_uom_qty:
    #             line.price_show=line.price_total/line.product_uom_qty

    @api.onchange('product_uom_qty', 'product_uom', 'route_id')
    def _onchange_product_id_check_availability(self):
        pass



